package com.ffapp.rainmonitorcustom.alarm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.bean.AlarmListData;
import com.ffapp.rainmonitorcustom.bean.AlarmProcessmanListBean;
import com.ffapp.rainmonitorcustom.bean.AlarmProcessmanListData;
import com.ffapp.rainmonitorcustom.bean.BaseListBeanYL;
import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 告警详情
 */
public class AlarmlPMChooseActivity extends BasisActivity {


    AlarmListData mDetailBean;
    AlarmProcessmanListBean mListBean;
    ArrayList<AlarmProcessmanListData> listChoosed;

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    MyAdapterList mAdapter;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.alarm_detail_process_manchoose);

        GridLayoutManager layoutManagerP = new GridLayoutManager(mContext, 2);
        mRecyclerView.setLayoutManager(layoutManagerP);
        mRecyclerView.setVerticalScrollBarEnabled(true);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);
        setTitle("预警信息");

    }
    @OnClick(R.id.btn_ok)
    void ok(){
        Intent intent = new Intent();
        intent.putExtra("list",listChoosed);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        mListBean = new AlarmProcessmanListBean();
        setListView();

        mDetailBean = (AlarmListData) getIntent().getExtras().getSerializable("data");
        listChoosed = (ArrayList<AlarmProcessmanListData>) getIntent().getExtras().getSerializable("list");
        mRecyclerView.refresh();


    }


    private void getList() {
        RequestParams params = new RequestParams();
        showProgress();
        params.put(BaseListBeanYL.PAGE_NAME, mListBean.getNextPage());
        params.put(BaseListBeanYL.PAGE_SIZE_NAME, BaseListBeanYL.PAGE_SIZE);
        HttpRestClient.post(Constants.URL_ALARM_USER_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        AlarmProcessmanListBean listBean = (AlarmProcessmanListBean) result;
                        mListBean.addListBean(listBean);
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                        onListViewComplete();
                    }
                },
                0, AlarmProcessmanListBean.class);

    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }

    public class MyAdapterList extends XRecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.alarm_handle_manchoose_grid_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            AlarmProcessmanListData data = mListBean.getList().get(position);
            viewHolder.text_name.setText(data.getCOL_NAME());
            viewHolder.text_mobile.setText(data.getCOL_PHONE());
            viewHolder.list_item.setSelected(mListBean.isCheck(data,listChoosed));
            viewHolder.list_item.setTag(data);
        }


        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_mobile)
            public TextView text_mobile;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                AlarmProcessmanListData data = (AlarmProcessmanListData) v.getTag();
//                data.setCheck(!data.isCheck());
                mListBean.addOrDelete(data,listChoosed);
                notifyDataSetChanged();
            }


        }
    }


}
